'''
Author: SimonCK666 SimonYang223@163.com
Date: 2022-05-18 19:50:42
LastEditors: SimonCK666 SimonYang223@163.com
LastEditTime: 2022-05-18 21:48:38
FilePath: \tao-bao-crawler\pkl2csv.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from email import header
import pickle
import pandas as pd
import datetime
import os
import csv

today=datetime.date.today()
# print(today)

fname = "小军鼓.pkl"
fnlist = fname.split('.')
# print(fnlist)
filename = fnlist[0]
# print(filename)
f = open(fname,'rb')
data = pickle.load(f)
pd.set_option('display.width',None)
pd.set_option('display.max_rows',None)
pd.set_option('display.max_colwidth',None)
# print(data)
inf=str(data)

foldername = str(today) + 'csv/'

if foldername.split('/')[0] not in os.listdir():
    os.mkdir(str(today) + 'csv/')
    
resname = str(today) + 'csv/' + str(today) + filename + '.csv'

headera = list(data[1].keys())
print(headera)

for item in data:
    res = []
    # print(data[item])
    res.append(data[item])
    with open(resname, 'a', newline='',encoding='utf-8-sig') as f:
        # headera = list(data[item].keys())
        writer = csv.DictWriter(f, fieldnames=headera)  # 提前预览列名，当下面代码写入数据时，会将其一一对应。
        writer.writeheader()  # 写入列名
        writer.writerows(res)  # 写入数据
    


print(resname + " DONE!")

f.close()
